package com.example.flickrphotoapp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;



public class PhotosList extends ListActivity
{

	private List<String> mListOfStrings = new ArrayList<String>();

	private List<Photo> mListOfPhotos = new ArrayList<Photo>();

	private FlickrPhotoResponse mFlickrPhotoResponse = null;

	private ListView mListView;
	
	private ProgressDialog mProgressBar;

	private class DataLoadingTask extends AsyncTask<Integer, Integer, Integer>
	{
		@Override
		protected void onPreExecute()
		{
//			 Toast t = new Toast(PhotosList.this);
//			 t.makeText(getApplicationContext(), "Flickr is downloading", Toast.LENGTH_SHORT);
//			 t.show();
		}

		@Override
		protected Integer doInBackground(Integer... arg0)
		{

			try
			{
				read();

			} catch (IOException e)
			{
				e.printStackTrace();
			}

			return null;

		}

		@Override
		protected void onProgressUpdate(Integer... values)
		{
			// Do nothing
			super.onProgressUpdate(values);
		}

		@Override
		protected void onPostExecute(Integer result)
		{
			display();
		}

	}

	protected void read() throws IOException
	{
		String jsonFileUrl = FeaturesConfiguration.getFlickrurl();
		mFlickrPhotoResponse = ObjectsReader
				.readFlickrPhotoResponse(jsonFileUrl);
	}

	protected void display()
	{
		MyCustomAdapter adapter = new MyCustomAdapter(this,
				R.layout.activity_list_row, mListOfPhotos);
		mListOfPhotos = mFlickrPhotoResponse.getFlickrPhotoList();
		mListView.setAdapter(adapter);
	}

	public class MyCustomAdapter extends ArrayAdapter<Photo>
	{
		public MyCustomAdapter(Context context, int textViewResourceId,
				List<Photo> mListOfPhotos)
		{
			super(context, textViewResourceId, mListOfPhotos);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			// TODO Auto-generated method stub
			// return super.getView(position, convertView, parent);

			View row = convertView;

			String strFileDate = null;

			String startDate = null;

			String startTime = null;

			if (row == null)
			{
				LayoutInflater inflater = getLayoutInflater();
				row = inflater.inflate(R.layout.row_photos, parent, false);
			}

			TextView list_name = (TextView) row.findViewById(R.id.list_name);

			TextView list_tagline = (TextView) row
					.findViewById(R.id.list_tagline);

			list_name.setText(mListOfPhotos.get(position).getTitle());

			list_tagline.setText("Owner : "
					+ mListOfPhotos.get(position).getOwner());

			ImageView imageView1 = (ImageView) row
					.findViewById(R.id.imageView1);

			
			// Drawable myAvatar =
			// SecondActivity.this.getResources().getDrawable(R.drawable.avatar_male_72x72);
			//
			// int resID =
			// getBaseContext().getResources().getIdentifier("avatar_male_72x72",
			// "drawable", "com.example.com.dastest.myapp");
			//
			// contactlogo.setImageResource(resID);

			return row;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_photos_list);
		mListOfStrings.add("Gangtok");
		mListOfStrings.add("Darjeeling");
		mListOfStrings.add("Chennai");

		Photo p1 = new Photo();
		p1.setTitle("Gangtok");

		Photo p2 = new Photo();
		p2.setTitle("Darjeeling");

		Photo p3 = new Photo();
		p3.setTitle("Chennai");

		Photo p4 = new Photo();
		p4.setTitle("Gangtok");

		Photo p5 = new Photo();
		p5.setTitle("Darjeeling");

		Photo p6 = new Photo();
		p6.setTitle("Chennai");

		Photo p7 = new Photo();
		p7.setTitle("Gangtok");

		Photo p8 = new Photo();
		p8.setTitle("Darjeeling");

		Photo p9 = new Photo();
		p9.setTitle("Chennai");

		Photo p10 = new Photo();
		p10.setTitle("Gangtok");

		Photo p11 = new Photo();
		p11.setTitle("Darjeeling");

		Photo p12 = new Photo();
		p12.setTitle("Chennai");

		mListOfPhotos.add(p1);
		mListOfPhotos.add(p2);
		mListOfPhotos.add(p3);

		mListOfPhotos.add(p4);
		mListOfPhotos.add(p5);
		mListOfPhotos.add(p6);

		mListOfPhotos.add(p7);
		mListOfPhotos.add(p8);
		mListOfPhotos.add(p9);

		mListOfPhotos.add(p10);
		mListOfPhotos.add(p11);
		mListOfPhotos.add(p12);

		MyCustomAdapter adapter = new MyCustomAdapter(this,
				R.layout.activity_list_row, mListOfPhotos);

		mListView = this.getListView();

		mListView.setAdapter(adapter);

		new DataLoadingTask().execute();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.photos_list, menu);
		return true;
	}

}
